package com.storeapp.ui;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.INullSelectionListener;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;

import com.storeapp.entity.domain.MyCustomer;
import com.storeapp.service.MycustomerService;
import com.storeapp.util.SpringUtils;

public class CustomerEditView extends ViewPart implements INullSelectionListener {
	public static final String ID = "com.storeapp.ui.CustomerEditView";

	private Text nameText;
	private Text birthdayText;
	private Text genderText;
	private Text phoneText;
	private Text emailText;

	private Label nameInvalidLabel;
	private Label birthdayInvalidLabel;
	private Label pvdrInvalidLabel;
	private Label genderInvalidLabel;
	private Label phoneInvalidLabel;
	private Label emailInvalidLabel;

	private ArrayList<IPropertyChangeListener> listeners;
	public static String actionType;
	private MyCustomer customer;
	private MyCustomer oldCustomer;

	private MycustomerService customerService;

	public CustomerEditView() {
		customer = new MyCustomer();
		oldCustomer = new MyCustomer();
		this.customerService = (MycustomerService) SpringUtils
				.getApplicationContext().getBean("mycustomerService");
		this.listeners = new ArrayList<IPropertyChangeListener>();
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		parent.setLayout(new GridLayout(5, true));
		
		// customer name
		Label nameLabel = new Label(parent, SWT.NONE);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		nameLabel.setText("姓名：");
		nameText = new Text(parent, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));
		nameInvalidLabel = new Label(parent, SWT.NONE);
		
		//birthday
		Label birthdayLabel = new Label(parent, SWT.NONE);
		birthdayLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		birthdayLabel.setText("生日：");
		birthdayText = new Text(parent, SWT.BORDER);
		birthdayText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));
		birthdayInvalidLabel = new Label(parent, SWT.NONE);
		
		//gender
		Label genderLabel = new Label(parent, SWT.NONE);
		genderLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		genderLabel.setText("性别：");
		genderText = new Text(parent, SWT.BORDER);
		genderText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));
		genderInvalidLabel = new Label(parent, SWT.NONE);
		
		//phone
		Label phoneLabel = new Label(parent, SWT.NONE);
		phoneLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		phoneLabel.setText("联系电话：");
		phoneText = new Text(parent, SWT.BORDER);
		phoneText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));
		phoneInvalidLabel = new Label(parent, SWT.NONE);
		
		//email
		Label emailLabel = new Label(parent, SWT.NONE);
		emailLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		emailLabel.setText("邮箱：");
		emailText = new Text(parent, SWT.BORDER);
		emailText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));
		emailInvalidLabel = new Label(parent, SWT.NONE);
		
		
		// button
		Label placeHolderLeft = new Label(parent, SWT.NONE);
		placeHolderLeft.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		
		Button saveButton = new Button(parent, SWT.NONE);
		saveButton.setText("确定");
		saveButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (actionType.equals("update")) {
					oldCustomer = customer;
				}
				
				customer.setCustomerName(nameText.getText());
			    customer.setBirthdat(Date.valueOf(birthdayText.getText()));
				customer.setGender(genderText.getText());
				customer.setPhone(phoneText.getText());
				customer.setEmail(emailText.getText());
				customerService.saveOrUpdateMycustomer(customer);
				notifyListeners();
			}
		});

		Button cancelButton = new Button(parent, SWT.NONE);
		cancelButton.setText("取消");
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				IViewReference view = getSite().getPage().findViewReference(CustomerEditView.ID);
				getSite().getPage().hideView(view);
			}
		});
		Label placeHolderRight = new Label(parent, SWT.NONE);

		if (actionType.equals("update")) {
			getSite().getPage().addSelectionListener(CustomerList.ID, this);
			fillText();
		}

		// add property listener to notify orderlist view to update list
		addPropertyChangeListener((IPropertyChangeListener) getSite()
				.getWorkbenchWindow().getActivePage().findView(CustomerList.ID));
	}

	// A public method that allows listener registration
	public void addPropertyChangeListener(IPropertyChangeListener listener) {
		if (!listeners.contains(listener))
			listeners.add(listener);
	}

	// A public method that allows listener registratINullSelectionListenerion
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		listeners.remove(listener);
	}

	public void notifyListeners() {
		// Post Invocation, inform listeners
		for (Iterator<IPropertyChangeListener> iter = listeners.iterator(); iter
				.hasNext();) {
			IPropertyChangeListener element = iter.next();
			if (actionType.equals("update")) {
				element.propertyChange(new PropertyChangeEvent(this,
						"updateCustomer", oldCustomer, customer));
			} else {
				element.propertyChange(new PropertyChangeEvent(this,
										"addCustomer", null, null));
			}

		}
	}

	private void fillText() {
		StructuredSelection sel = (StructuredSelection) getSite().getPage()
				.getSelection(CustomerList.ID);
		customer = (MyCustomer) sel.getFirstElement();
		nameText.setText(customer.getCustomerName());
		birthdayText.setText(String.format("%1$tY-%1$tm-%1$te", customer.getBirthdat()));
		genderText.setText(customer.getGender());
		phoneText.setText(customer.getPhone());
		emailText.setText(customer.getEmail());
		
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		// TODO Auto-generated method stub
		if (selection instanceof StructuredSelection
				&& ((StructuredSelection) selection).getFirstElement() instanceof MyCustomer) {
			customer = (MyCustomer) ((StructuredSelection) selection)
					.getFirstElement();
			
		}
	}

	@Override
	public void dispose() {
		super.dispose();
		getSite().getPage().removeSelectionListener(CustomerList.ID, this);
	}
}
